var progect_type = null;
var work_type = null;
var voltage_type = null;
$(function () {
    $.when(getdatadictionary("progect_type"), getdatadictionary("work_type"), getdatadictionary("voltage_type")).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'api/bdevicequantities/list?dguid=' + GetUrlParam("dguid"),
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                {label: '设备信息ID', name: 'dGuid', index: 'd_guid', width: 80, hidden: true},
                {
                    label: '适用项目类别', name: 'dQType', index: 'd_q_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in progect_type) {
                            if (cellvalue == progect_type[d].dKey) {
                                names = progect_type[d].dValue;
                            }
                        }


                        return names;
                    }
                },
                {
                    label: '适用工作类别 ', name: 'dQWork', index: 'd_q_work', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in work_type) {
                            if (cellvalue == work_type[d].dKey) {
                                names = work_type[d].dValue;
                            }
                        }


                        return names;
                    }
                },
                {label: '工程量', name: 'dQQuantities', index: 'd_q_quantities', width: 80},
                {label: '取值说明', name: 'dQExplain', index: 'd_q_explain', width: 80},
                {label: '购置单位', name: 'dQUnit', index: 'd_q_unit', width: 80},
                {label: '计量单位', name: 'dQMeterUnit', index: 'd_q_meter_unit', width: 80},
                {label: '单价（元）', name: 'dQPrice', index: 'd_q_price', width: 80},
                {label: '计量与计价比值', name: 'dQRatio', index: 'd_q_ratio', width: 80},
                {label: '备注', name: 'dQRemarks', index: 'd_q_remarks', width: 80}
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    )
});

function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
        if (sss == "work_type") {
            work_type = r.returnlist;
        }
        if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }

        vm.work_type = work_type;
        vm.progect_type = progect_type;

    });
}
function getworktype(txt, v) {
    if (v == 1) {
        for (d in work_type) {
            if (work_type[d].dKey == txt) return work_type[d].dValue;
        }
    } else if (v == 2) {
        for (d in voltage_type) {
            if (voltage_type[d].dKey == txt) return voltage_type[d].dValue;
        }
    }
    return "";

}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            dQWork: null,
            dQType: null
        },
        showList: true,
        title: null,
        dGuid: GetUrlParam("dguid"),
        bDeviceQuantities: {},
        progect_type: progect_type,
        work_type: work_type

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bDeviceQuantities = {dGuid: GetUrlParam("dguid")};

            vm.progect_type = progect_type;
            vm.work_type = work_type;
            vm.getDept(GetUrlParam("dguid"));

        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.dGuid = GetUrlParam("dguid");
            vm.progect_type = progect_type;
            vm.work_type = work_type;
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bDeviceQuantities.guid == null ? "api/bdevicequantities/save" : "api/bdevicequantities/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bDeviceQuantities),
                success: function (r) {
                    if (r.code === 0) {
                        if(vm.bDeviceQuantities.guid == null)
                        {

                            confirmaa('操作成功，是否继续添加？', function (r) {

                                if(r=="ok")
                                {

                                    vm.add();
                                }
                                if(r=="no")
                                {
                                   vm.reload();
                                }
                            });

                        }
                        else
                        {
                            alert('操作成功', function (index) {

                                vm.reload();
                            });
                        }

                    }

                     else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bdevicequantities/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        addall:function(){

            confirm('确定要添加全部工程量？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bdevicequantities/addall",
                    contentType: "application/json",
                    data: JSON.stringify( GetUrlParam("dguid")),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        getInfo: function (guid) {
            $.get(baseURL + "api/bdevicequantities/info/" + guid, function (r) {
                vm.bDeviceQuantities = r.bDeviceQuantities;
                vm.bDeviceQuantities.dGuid = GetUrlParam("dguid");

            });
        },
        getDept: function (guid) {
            //加载部门树
            $("#jqGrid1").jqGrid({
                url: baseURL + 'api/bbudgetitem/list?dQuery=' + GetUrlParam("dQuery") + "&dClass="
                + GetUrlParam("dClass") + "&dItem=" + GetUrlParam("dItem"),
                datatype: "json",
                colModel: [
                    {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                    {label: '所属预算标准ID', name: 'bGuid', index: 'b_guid', width: 80, hidden: true},
                    {
                        label: '项目类别',
                        name: 'bIType',
                        index: 'b_i_type',
                        width: 80,
                        formatter: function (cellvalue, options, rowObject) {
                            var names = "";

                            for (d in progect_type) {
                                if (cellvalue == progect_type[d].dKey) {
                                    names = progect_type[d].dValue;
                                }
                            }


                            return names;
                        }
                    },
                    {label: '预算项目分类', name: 'bIClass', index: 'b_i_class', width: 80},
                    {label: '预算项目', name: 'bIItem', index: 'b_i_item', width: 80},

                    {
                        label: '工作类容',
                        name: 'bIWork',
                        index: 'b_i_work',
                        width: 80,
                        formatter: function (cellvalue, options, rowObject) {
                            var names = "";

                            for (d in work_type) {
                                if (cellvalue == work_type[d].dKey) {
                                    names = work_type[d].dValue;
                                }
                            }


                            return names;
                        }
                    },
                    {
                        label: '电压等级',
                        name: 'bIVoltage',
                        index: 'b_i_voltage',
                        width: 80, formatter: function (cellvalue, options, rowObject) {
                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                        }
                        return "";
                    }
                    },
                    {label: '预算参数', name: 'bIParameter', index: 'b_i_parameter', width: 80},
                    {label: '计价单位', name: 'bIUnit', index: 'b_i_unit', width: 80},
                    {label: '取值下限', name: 'bILower', index: 'b_i_lower', width: 80},
                    {label: '取值上限', name: 'bIHigh', index: 'b_i_high', width: 80},
                    {label: '价格', name: 'bIPrice', index: 'b_i_price', width: 80},
                    {label: '取值方式', name: 'bIValue', index: 'b_i_value', width: 80},
                    {label: '说明', name: 'bIExplain', index: 'b_i_explain', width: 80}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList: [10, 30, 50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth: true,
                multiselect: true,
                pager: "#jqGridPager1",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#jqGrid1").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                }
            })
        },
        deptTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择预算项",
                area: ['700px', '500px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    console.log(index)
                    var grid = $("#jqGrid1");
                    var rowKey = grid.getGridParam("selrow");
                    if (!rowKey) {
                        alert("请选择一条记录");
                        return;
                    }

                    var guids = grid.getGridParam("selarrrow");
                    console.log(guids)
                    if (guids.length > 1) {
                        alert("只能选择一条记录");
                        return;
                    }

                    $.get(baseURL + "api/bbudgetitem/info/" + guids[0], function (r) {
                       var qzsm="";
                        if (r.bBudgetItem.bIVoltage!=null&&r.bBudgetItem.bIVoltage!="")
                       {
                           qzsm+="电压等级:"+ getworktype(r.bBudgetItem.bIVoltage, 2)+"   ";
                       }
                       if( r.bBudgetItem.bIParameter!=null&&r.bBudgetItem.bIParameter!="")
                       {
                           qzsm += r.bBudgetItem.bIParameter + ":" + GetUrlParam("dQuery")+"  ";
                       }
                        vm.bDeviceQuantities = {
                            dQRatio: 1,
                            dGuid: GetUrlParam("dguid"),
                            dQType: r.bBudgetItem.bIType,
                            dQWork: r.bBudgetItem.bIWork,
                            dQPrice: r.bBudgetItem.bIPrice,
                            dQMeterUnit: r.bBudgetItem.bIUnit,
                            dQQuantities: r.bBudgetItem.bIClass+"-"+ r.bBudgetItem.bIItem +"-"+ getworktype(r.bBudgetItem.bIWork, 1),
                            dQUnit: GetUrlParam("dUnit"),
                            dQExplain: qzsm
                            //
                            // {label: '取值说明', name: 'dQExplain', index: 'd_q_explain', width: 80},
                            //  {label: '购置单位', name: 'dQUnit', index: 'd_q_unit', width: 80},
                        };

                    });
                    layer.close(index);

                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'dQWork': vm.q.dQWork,
                    'dQType': vm.q.dQType
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});